<template>
  <el-dialog
    title="新增"
    :close-on-click-modal="false"
    width="50%"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="divisordataForm"
      label-width="200px"
    >
    <div class="row">
      <el-form-item label="因子名称" prop="qualityName" class="col-md-10">
        <el-input
          v-model="dataForm.qualityName"
          placeholder="因子名称"
        ></el-input>
      </el-form-item>
    </div>
      <div class="row">
        <el-form-item label="因子标识" prop="qualityIdentify" class="col-md-10">
        <el-input
          v-model="dataForm.qualityIdentify"
          placeholder="因子标识"
        ></el-input>
      </el-form-item>
      </div>
      <div class="row">
      <el-form-item label="是否有效" prop="isEffect" class="col-md-12">
        <el-switch
          v-model="dataForm.isEffect"
          active-color="#13ce66"
          inactive-color="#ff4949"
          on-value="1" on-text="是" off-text="否" off-value="0"
          @change="effectChange"
        >
        </el-switch>
      </el-form-item>
      </div>

      
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button :plain="true" type="info" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveData, checkRepeat } from "@/api/baseConfig/divisor";
import constant from "../../../../../api/constant";

export default {
  data() {
    var validateIdentify = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("标识不能为空"));
      } else {
        var patrn = /^[a-z][a-zA-Z0-9]{0,50}$/;
        // console.log(value);
        if (!patrn.test(value)) {
          callback(new Error("标识由字母和数字组成，且首字母为小写长度小于50"));
        } else {
          checkRepeat({ qualityIdentify: value }).then((res) => {
            // console.log(res);
            if (res.data.resultCode == 0) {
              callback();
            } else {
              callback(new Error(res.data.resultMsg));
            }
          });
          // callback();
        }
      }
    };
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("名称不能为空"));
      } else {
        var regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im;
        var regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im;

        if (regEn.test(value) || regCn.test(value) || value.length >= 50) {
          callback(new Error("名称不能包含特殊字符，且长度小于50"));
        } else {
          checkRepeat({ qualityName: value }).then((res) => {
            // console.log(res);
            if (res.data.resultCode == 0) {
              callback();
            } else {
              callback(new Error(res.data.resultMsg));
            }
          });
        }
      }
    };
    return {
      visible: false,
      dataForm: {
        qualityId: null,
        qualityName: "",
        qualityType: "",
        qualityIdentify: "",
        isEffect: "1",
      },
      entityItemType: constant.ruleConfig.entityItemType,
      dataRule: {
        qualityName: [{ validator: validateName, trigger: "blur" }],
        qualityIdentify: [{ validator: validateIdentify, trigger: "blur" }],
      },
    };
  },
  created() {},
  methods: {
    effectChange(status) {
      // console.log(status);
      this.dataForm.isEffect = status ? "1" : "0";
    },
    //初始化
    init(id) {
      this.dataForm = {
        qualityId: null,
        qualityName: "",
        qualityIdentify: "",
        qualityType: "",
        isEffect: "1",
      };
      this.visible = true;

      this.$nextTick(() => {
        this.$refs["divisordataForm"].resetFields();
        if (id) {
          this.dataForm.qualityId = id;
          let params = {
            id: this.dataForm.qualityId + "",
          };
          // this.$store.dispatch('loading/CHANGE_LOADING', true)
          // getData(params).then(res => {
          //     this.$store.dispatch('loading/CHANGE_LOADING', false)
          //     if (res.data.data && res.data.resultCode === 0) {
          //         this.dataForm = res.data.data
          //     }
          // }).catch(err=>{
          //     this.$store.dispatch('loading/CHANGE_LOADING', false)
          // })
        }
      });
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["divisordataForm"].validate((valid) => {
        if (valid) {
          this.$store.dispatch("loading/CHANGE_LOADING", true);
          saveData(JSON.stringify(this.dataForm))
            .then((res) => {
              this.$store.dispatch("loading/CHANGE_LOADING", false);
              if (res.data && res.data.resultCode === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.dataForm = {
                      qualityId: null,
                      qualityIdentify: "",
                      qualityName: "",
                      qualityType: "",
                      isEffect: "1",
                    };
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(res.data.resultMsg);
              }
            })
            .catch((err) => {
              this.$store.dispatch("loading/CHANGE_LOADING", false);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss">
.title {
  padding: 10px;
  background-color: rgba(248, 248, 248, 1);
  margin-bottom: 10px;
}

.el-table__row .el-form-item__content {
  margin-left: 0 !important;
}

.el-table__row .el-form-item {
  margin-bottom: 0 !important;
}

</style>
